package com.wei.czz.framework.admin.controller;

import com.wei.czz.common.annotation.Auth;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.admin.UserDto;
import com.wei.czz.common.jsr303.group.Search;
import com.wei.czz.common.property.BatchProperty;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.admin.user.BindUserVo;
import com.wei.czz.common.vo.admin.userRoleGroup.UserRoleGroupFormVo;
import com.wei.czz.framework.admin.manager.UserRoleGroupManager;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-10-30 21:10:07
 * className: UserRoleGroupController 用户与角色授权组关联接口类
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/sys/user-role-group")
@AllArgsConstructor
public class UserRoleGroupController {

    private static final Logger log = LoggerFactory.getLogger(UserRoleGroupController.class);

    private final BatchProperty batchProperty;

    private final UserRoleGroupManager userRoleGroupManager;

    /**
     * 角色授权组绑定用户接口
     * @param bindUserVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'user-role-group-bind' + #userPo.userId", message = "角色授权组绑定用户")
    @PostMapping("/bind")
    @Auth(permit = "system:role-space:save")
    public Result<Object> bindRole(@Validated @RequestBody BindUserVo bindUserVo) {
        log.info("开始角色授权组绑定用户业务。{}", bindUserVo);

        List<Long> userIdList = bindUserVo.getUserIdList();
        if (batchProperty.getMaxSize() < userIdList.size()) {
            log.warn("角色授权组绑定用户数量超限。batch.maxSize={} list.size={}", batchProperty.getMaxSize(), userIdList.size());
            return Result.clientFail("一次绑定用户，最多允许同时绑定" + batchProperty.getMaxSize() + "个用户");
        }

        userRoleGroupManager.bindUser(bindUserVo);

        log.info("角色授权组绑定用户业务结束");
        return Result.success();
    }

    /**
     * 分页获取角色授权组关联用户接口
     * @param formVo 表单对象
     * @return 结果
     */
    @PostMapping("/page/list")
    @Auth(permit = "system:role-space:save")
    public Result<PageDto<UserDto>> getPageList(@Validated(Search.class) @RequestBody UserRoleGroupFormVo formVo) {
        log.info("开始分页获取角色授权组关联用户业务。{}", formVo);

        PageDto<UserDto> pageDto = userRoleGroupManager.getPageList(formVo);

        log.info("分页获取角色授权组关联用户业务结束");
        return Result.success(pageDto);
    }

    /**
     * 角色授权组解绑用户接口
     * @param bindUserVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'user-role-group-unbind' + #userPo.userId", message = "角色授权组解绑用户")
    @PostMapping("/unbind")
    @Auth(permit = "system:role-space:save")
    public Result<Object> unbindRole(@Validated @RequestBody BindUserVo bindUserVo) {
        log.info("开始角色授权组解绑用户业务。{}", bindUserVo);

        List<Long> userIdList = bindUserVo.getUserIdList();
        if (batchProperty.getMaxSize() < userIdList.size()) {
            log.warn("角色授权组解绑用户数量超限。batch.maxSize={} list.size={}", batchProperty.getMaxSize(), userIdList.size());
            return Result.clientFail("一次绑定用户，最多允许同时解绑" + batchProperty.getMaxSize() + "个用户");
        }

        userRoleGroupManager.unbindUser(bindUserVo);

        log.info("角色授权组解绑用户业务结束");
        return Result.success();
    }
}
